/*
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU Lesser General Public License Version
 *  3 as published by the Free Software Foundation.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307,
 *  USA.
 */

#include "systeminterface.h"

#include <optra/common/global.h>
#include <QDir>
#include <QTextStream>
#include <QSysInfo>
#include <QDesktopServices>
#include "QtSystemInfo.h"

SystemInterface::SystemInterface(QObject *parent):
        ZoneInterface(parent)
{
    info_ = new Optra::ZoneInfo("system", "System");
    info_->setIcon(QIcon(":/icons/main/systemMainIcon"));
}

SystemInterface::~SystemInterface(void)
{
    delete info_;
}

Optra::ZoneInfo* SystemInterface::zoneInfo()
{
    return info_;
}

QString systemTypeToString(QtSystemInfo::SystemType type)
{
    switch (type)
    {
    case QtSystemInfo::UnknownType:
        return "UnknownType";
    case QtSystemInfo::UnknownUnix:
        return "UnknownUnix";
    case QtSystemInfo::MacOSX:
        return "MacOSX";
    case QtSystemInfo::Windows:
        return "Windows";
    case QtSystemInfo::WindowsCE:
        return "WindowsCE";
    case QtSystemInfo::Linux:
        return "Linux";
    case QtSystemInfo::UnknownBSD:
        return "UnknownBSD";
    case QtSystemInfo::FreeBSD:
        return "FreeBSD";
    case QtSystemInfo::OpenBSD:
        return "OpenBSD";
    case QtSystemInfo::NetBSD:
        return "NetBSD";
    }

    return "Bad input value!";
}

QString architectureToString(QtSystemInfo::ArchitectureType type)
{
    switch (type)
    {
    case QtSystemInfo::UnknownArchitectureType:
        return "UnknownArchitectureType";
    case QtSystemInfo::I386:
        return "I386";
    case QtSystemInfo::I486:
        return "I486";
    case QtSystemInfo::I586:
        return "I586";
    case QtSystemInfo::I686:
        return "I686";
    case QtSystemInfo::X86_64:
        return "X86_64";
    case QtSystemInfo::PPC:
        return "PPC";
    case QtSystemInfo::PPC64:
        return "PPC64";
    }

    return "Bad input value!";
}

QVariantMap SystemInterface::version()
{
    QVariantMap ret;

    QtSystemInfo systemInfo(this);

    ret.insert("--", "base");
    ret.insert("version", "0.0.1");

    ret.insert("--", "additional");
    ret.insert("System name", systemInfo.systemName());
    ret.insert("System version", systemInfo.systemVersion());

    ret.insert("--", "advanced");
    ret.insert("Kernel name", systemInfo.kernelName());
    ret.insert("Kernel version", systemInfo.kernelVersion());
    ret.insert("Architecture name", systemInfo.architectureName());
    ret.insert("System type", systemTypeToString(systemInfo.systemType()));
    ret.insert("Architecture type", architectureToString(systemInfo.architecture()));

    return ret;
}

QStringList SystemInterface::directoryContent(const QString& directory)
{
    if (directory == "desktop"){
        QDir desktop(QDesktopServices::storageLocation(QDesktopServices::DesktopLocation));
        return desktop.entryList(QDir::Files, QDir::Time);
    }else if (directory == "documents"){
        QDir desktop(QDesktopServices::storageLocation(QDesktopServices::DocumentsLocation));
        return desktop.entryList(QDir::Files | QDir::NoDotAndDotDot | QDir::NoSymLinks | QDir::Dirs, QDir::Time);
    }else if (directory == "cache"){
        QDir desktop(QDesktopServices::storageLocation(QDesktopServices::CacheLocation));
        return desktop.entryList(QDir::Files | QDir::NoDotAndDotDot | QDir::NoSymLinks, QDir::Time);
    }

    QDir dir(directory);
    return dir.entryList(QDir::NoDotAndDotDot | QDir::Readable, QDir::Name);
}

bool SystemInterface::initialize()
{
    ui_ = new SystemUI(proxy());

   // Optra::ZoneWindow* w = new Optra::ZoneWindow("system#FileSystem", "File System", ui_);
   // w->setIcon(QIcon(":/icons/main/systemMainIcon"));
   // info_->addView(w);
    debug("Initialize Zone System");
    updateState(Op::Ready);
    return true;
}

void SystemInterface::launch()
{
    info("System Launch");
}
